package com.example.text.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.text.common.result.Result;
import com.example.text.common.result.ResultFactory;
import com.example.text.entity.ExamineEntity;
import com.example.text.entity.FuleassociationEntity;
import com.example.text.entity.FulewinEntity;
import com.example.text.service.ExamineService;
import com.example.text.service.FuleassociationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class ImplementController {
    @Autowired
    ExamineService examineService;

    @Autowired
    FuleassociationService fuleassociationService;
    //显示中标下达表
    @RequiresPermissions("/api/admin/implement")
    @GetMapping("/api/admin/implement/{size}/{page}")
    public Result listUsers(@PathVariable("size") int size, @PathVariable("page") int page) throws Exception {
        System.out.println("一页有："+size+"个数据");
        System.out.println("从第"+page+"个页开始");
        Page<ExamineEntity> pages = new Page<>(page,size);
        return ResultFactory.buildSuccessResult(examineService.selectExaminePage(pages,2));
    }

    //根据编号就显示中标下达表
    @RequiresPermissions("/api/admin/implement")
    @GetMapping("/api/admin/implement/{size}/{page}/{id}")
    public Result listUsersbyWord(@PathVariable("size") int size, @PathVariable("page") int page,
                                  @PathVariable("id") String id) throws Exception {
        System.out.println("一页有："+size+"个数据"+"id:"+id);
        System.out.println("从第"+page+"个页开始");
        Page<ExamineEntity> pages = new Page<>(page,size);
        if(examineService.selectExaminePagebyWord(pages,id,2)==null){
            return ResultFactory.buildFailResult("没有该编号，请重新输入");
        }else {
            return ResultFactory.buildSuccessResult(examineService.selectExaminePagebyWord(pages, id,2));
        }
    }

    //根据状态就显示中标下达表
    @RequiresPermissions("/api/admin/implement")
    @GetMapping("/api/admin/implement/{size}/{page}/stat/{status}")
    public Result listUsersbyStatus(@PathVariable("size") int size, @PathVariable("page") int page,
                                    @PathVariable("status") int stutas) throws Exception {
        System.out.println("一页有："+size+"个数据"+"id:"+stutas);
        System.out.println("从第"+page+"个页开始");
        Page<ExamineEntity> pages = new Page<>(page,size);

        return ResultFactory.buildSuccessResult(examineService.selectExaminePagebyStatus(pages, stutas,2));

    }
    //删除中标下达表
    @PostMapping("/api/admin/implement/delete")
    public Result delete(@RequestBody ExamineEntity examineEntity) throws Exception {

        //修改上一级的任务：采购单审核
        //先获取上一级的数据
        ExamineEntity entityDB=examineService.getById(examineEntity.getId());
        int fuleapplyId=entityDB.getFuleapplyid();
        int audit_status=2;
        int status=1;
        //查询上一级数据
        ExamineEntity last= examineService.lastExamine(fuleapplyId,audit_status,status);
        //修改上一级状态
        last.setRemark("");
        last.setAuditStatus(1);
        last.setCreateTime(null);
        examineService.updateById(last);
        examineService.removeById(examineEntity.getId());
        return ResultFactory.buildSuccessResult("删除成功");


    }
    //批量删除中标下达表
    @PostMapping("/api/admin/implement/deletes/{ids}")
    public Result deletePurchase(@PathVariable("ids") String ids){
        System.out.println("ids"+ids);
        String[] AdvertiseIds = ids.split("\\,");
        examineService.removeByIds(Arrays.asList(AdvertiseIds));
        return ResultFactory.buildSuccessResult("批量中标下达表删除成功");
    }

    //中标下达表下达
    @CrossOrigin
    @PutMapping("api/admin/implement/status/{username}")
    public Result updateAdvertiseStatus(@RequestBody ExamineEntity requestExamine,@PathVariable("username") String username ) {
        System.out.println("传入的值为："+requestExamine+"执行者为："+username);
        System.out.println("执行者为："+username);
        ExamineEntity examineEntity=examineService.updateImplementStatus(requestExamine,username);
        String message = "审核成功";
        return ResultFactory.buildSuccessResult(message);
    }

    //显示中标结果
    @GetMapping("/api/admin/implement/fulewin/{id}")
    public Result SelectFulewinById(@PathVariable("id") int id){
        List<FulewinEntity> fulewinEntities= fuleassociationService.FulewinByFuleapplyid(id);
        return ResultFactory.buildSuccessResult(fulewinEntities);
    }

    
}
